cmake_minimum_required(VERSION 3.13)
project(Hello2D)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Release")
endif ()

if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR $CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64")
    set(ARCH x64)
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
    set(ARCH arm64)
else ()
    set(ARCH x86)
endif ()

file(GLOB SWIFTSHADER_LIBS ../vendor/swiftshader/mac/${ARCH}/*.${CMAKE_SHARED_LIBRARY_SUFFIX})
list(APPEND HELLO_2D_LIBS ${SWIFTSHADER_LIBS})
list(APPEND HELLO_2D_INCLUDES ../vendor/swiftshader/include)

if (APPLE)
    find_library(APPLICATION_SERVICES_FRAMEWORK ApplicationServices REQUIRED)
    list(APPEND HELLO_2D_LIBS ${APPLICATION_SERVICES_FRAMEWORK})
    find_library(QUARTZ_CORE QuartzCore REQUIRED)
    list(APPEND HELLO_2D_LIBS ${QUARTZ_CORE})
    find_library(COCOA Cocoa REQUIRED)
    list(APPEND HELLO_2D_LIBS ${COCOA})
    find_library(FOUNDATION Foundation REQUIRED)
    list(APPEND HELLO_2D_LIBS ${FOUNDATION})
    find_library(ICONV_LIBRARIES NAMES iconv libiconv libiconv-2 c)
    list(APPEND HELLO_2D_LIBS ${ICONV_LIBRARIES})
    find_library(CORE_MEDIA CoreMedia)
    list(APPEND HELLO_2D_LIBS ${CORE_MEDIA})
else ()
    find_package(Threads)
    list(APPEND HELLO_2D_LIBS ${CMAKE_THREAD_LIBS_INIT})
    list(APPEND HELLO_2D_LIBS dl)
    list(APPEND HELLO_2D_COMPILE_OPTIONS -fPIC -pthread)
endif ()

set(TGFX_BUILD_DRAWERS ON)
set(TGFX_USE_SWIFTSHADER ON)
set(TGFX_USE_FREETYPE ON)

set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../ ./tgfx)

file(GLOB_RECURSE HELLO_2D_SOURCE_FILES src/*.*)
add_executable(Hello2D ${HELLO_2D_SOURCE_FILES})
target_include_directories(Hello2D PRIVATE ${HELLO_2D_INCLUDES} src)
target_link_libraries(Hello2D tgfx-drawers tgfx ${HELLO_2D_LIBS})

